#include "XMLReader.h"

XMLReader::XMLReader(void)
	: delim("/ ")
{
	game.LoadFile("Assets/game.xml");
}

XMLReader::~XMLReader(void)
{

}

tinyxml2::XMLElement* XMLReader::GetElem(std::string path[], int count, bool value)
{
	tinyxml2::XMLElement* element;
	int countup = 0;

	element = game.FirstChildElement(path[countup].c_str());
	count--;
	countup++;

	//IF VALUE == true, Returns the before last element, if false, returns the element asked
	int retelem = 0;
	if(value) {retelem = 1;}

	while(count != retelem)
	{
		element = element->FirstChildElement(path[countup].c_str());
		count--;
		countup++;
	}

	return element;
}

int XMLReader::GetValue(std::string path)
{
	Tokenizer str(path, delim);
	std::string token;
	std::string strtab[10];
	int count = 0;

	while((token = str.next()) != "")
	{
		strtab[count] = token;
		count++;
	}

	return GetElem(strtab, count, true)->IntAttribute(strtab[count - 1].c_str());

}

tinyxml2::XMLElement* XMLReader::GetElement(std::string path)
{
	Tokenizer str(path, delim);
	std::string token;
	std::string strtab[10];
	int count = 0;

	while((token = str.next()) != "")
	{
		strtab[count] = token;
		count++;
	}

	return GetElem(strtab, count);
}

int XMLReader::GetValueFromElement(tinyxml2::XMLElement* doc, std::string value)
{
	return doc->IntAttribute(value.c_str());
}